<?php
namespace app\admin\controller;

use app\api\model\SwipeModel;

class Appmgr extends Base{
	/**
	 * [app轮播图]
	 */
	public function swipe() {
		if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$swipeModel = new SwipeModel();
            $data['rows'] = $swipeModel->limit($offset, $limit)->order('id desc')->select()->toArray();
			$data['total'] = $swipeModel->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
        }
		return $this->fetch();
	}
	/**
	 * [添加]
	 */
	public function swipeAdd() {
		$param = input("post.");
		if(mb_strlen($param['remark']) >32 || mb_strlen($param['remark']) < 1){
			return ["code"=>0,"msg"=>"备注的字符长度为1~32"];
		}
		$swipeModel = new SwipeModel();
		$swipeModel::startTrans();
		try {
			$swipeModel::insert($param);
			$swipeModel::commit();
			return ["code"=>1,"msg"=>"添加成功"];
		} catch (\Exception $e) {
			$swipeModel::rollback();
			return ['code'=>0,'msg'=>'添加失败:'.$e->getMessage()];
		}
	}
	/**
	 * [编辑]
	 */
	public function swipeEdit() {
		$param = input("post.");
		$swipeModel = new SwipeModel();
		$swipeModel::startTrans();
		try {
			if(!$param['url']){
				if(mb_strlen($param['remark']) >32 || mb_strlen($param['remark']) < 1){
					return ["code"=>0,"msg"=>"备注的字符长度为1~32"];
				}
			}else{
				$path = $swipeModel::where("id",$param['id'])->value("url");
				$path = APP_PATH."../public".$path;
				if(file_exists($path)){
					@unlink($path);
				}
			}
			$swipeModel::where("id",$param['id'])->update($param);
			$swipeModel::commit();
			return ["code"=>1,"msg"=>"编辑成功"];
		} catch (\Exception $e) {
			$swipeModel::rollback();
			return ['code'=>0,'msg'=>'编辑失败:'.$e->getMessage()];
		}
	}
	/**
	 * [删除]
	 */
	public function swipeDel() {
		$param = input("post.");
		$swipeModel = new SwipeModel();
		$swipeModel::startTrans();
		try {
			if($param['url']){
				$path = APP_PATH."../public".$param['url'];
				$path = iconv("UTF-8", "GBK", $path);
				if(file_exists($path)){
					@unlink($path);
				}
			}
			$swipeModel::where("id",$param['id'])->delete();
			$swipeModel::commit();
			return ["code"=>1,"msg"=>"删除成功"];
		} catch (\Exception $e) {
			$swipeModel::rollback();
			return ['code'=>0,'msg'=>'删除失败:'.$e->getMessage()];
		}
	}
}
